package com.itlaoqi.thread.threadpool;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolSample4 {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//        scheduledThreadPool.schedule(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("延迟3秒");
//            }
//        },3, TimeUnit.SECONDS);

        //循环执行
        //Timer，项目实际开发中scheduledThreadPool与Timer都不会用到，因为有成熟的框架Quartz。或者Spring自带的框架
        //程序的调用框架支持一种表达式Cron表达式。
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(new Date() + "延迟1秒执行，每3s执行一次");
            }
        },1,3,TimeUnit.SECONDS);
    }
}
